import { reactive, toRefs } from 'vue'

import { deptApi, type ITreeNode } from '@/apis'

export function useDeptTree() {
  const state = reactive({
    deptTree: [] as ITreeNode[],
    treeLoading: false
  })

  /**
   * 获取部门树
   */
  async function getDeptTree() {
    state.treeLoading = true

    state.deptTree = await deptApi.getDeptTree()

    state.treeLoading = false
  }

  return {
    ...toRefs(state),
    getDeptTree
  }
}
